package com.bxd.baodan.widget;


import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.util.ImageUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 图片选项，用于设置图片和边框
 * 
 * @author
 */
public class ImageOptionPreference extends Preference {

	private PreferenceActivity parent;
	private int rightImage;
	private ImageView imageView;
	private Drawable mRightImageDrawable;
	private Bitmap mBitmap = null;
	private int OPEN_ALBUM = 0;
	protected DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	// 构造函数
	public ImageOptionPreference(Context context) {
		super(context);
		options = new DisplayImageOptions.Builder()
//		.showImageForEmptyUri(R.drawable.connect).showImageOnFail(params)
		.cacheInMemory(true).cacheOnDisc(true).build();
	}

	public ImageOptionPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		options = new DisplayImageOptions.Builder()
//		.showImageForEmptyUri(R.drawable.connect).showImageOnFail(params)
		.cacheInMemory(true).cacheOnDisc(true).build();
//		TypedArray typedArray = context.obtainStyledAttributes(attrs,
//				R.styleable.preference);
//		rightImage = typedArray.getResourceId(R.styleable.preference_image, 0);
//		mRightImageDrawable = context.getResources().getDrawable(rightImage);
//		typedArray.recycle();
	}

	public ImageOptionPreference(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		options = new DisplayImageOptions.Builder()
//		.showImageForEmptyUri(R.drawable.connect).showImageOnFail(params)
		.cacheInMemory(true).cacheOnDisc(true).build();
//		TypedArray typedArray = context.obtainStyledAttributes(attrs,
//				R.styleable.preference, defStyle, 0);
//		rightImage = typedArray.getResourceId(R.styleable.preference_image, 0);
//		mRightImageDrawable = context.getResources().getDrawable(rightImage);
//		typedArray.recycle();
	}

	public void setActivity(PreferenceActivity parent) {
		this.parent = parent;
	}

	@Override
	public boolean isPersistent() {
		return false;
	}

	public void setImage(Bitmap newImage) {
		imageView.setImageBitmap(newImage);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		return LayoutInflater.from(getContext()).inflate(
				R.layout.preference_image, parent, false);
	}

	@Override
	protected void onBindView(View view) {
		super.onBindView(view);

		imageView = (ImageView) view.findViewById(R.id.RightImage);
		imageView.setImageDrawable(mRightImageDrawable);
		loadImage();
	}

	@Override
	protected void onClick() {
//		//跳转到activity
//		((BasePreferenceActivity)(this.getAty())).openActivity(ModifyPersonInfor.class);
	}
	public void loadImage(){
//		imageLoader.displayImage(SpUtil.getInstance().getString(SharedPreferanceKey.TEMPICURL), imageView,
//				options);
		ImageUtil.setAvatarToRounderCorner(getContext(), SpUtil.getInstance().getString(SharedPreferanceKey.TEMPICURL), imageView, true);
	}
	/**
	 * 选择图片裁剪
	 * 
	 * @param output
	 */
	protected void startImagePick() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image/*");
		parent.startActivityForResult(Intent.createChooser(intent, "选择图片"),
				ImageUtil.REQUEST_CODE_GETIMAGE_BYCROP);
	}
	protected void startActionCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, this.getCameraTempFile());
		parent.startActivityForResult(intent, ImageUtil.REQUEST_CODE_GETIMAGE_BYCAMERA);
	}

	// 拍照保存的绝对路径
		private Uri getCameraTempFile() {
			String storageState = Environment.getExternalStorageState();
			if (storageState.equals(Environment.MEDIA_MOUNTED)) {
				File savedir = new File(Constant.FILE_SAVEPATH);
				if (!savedir.exists()) {
					savedir.mkdirs();
				}
			} else {
//				super.showToast("无法保存上传的头像，请检查SD卡是否挂载");
				return null;
			}
			String timeStamp = StringUtil.getTimeStamp();
			// 照片命名
			String cropFileName = "helper_" + timeStamp + ".jpg";
			// 裁剪头像的绝对路径
			String protraitPath = Constant.FILE_SAVEPATH + "/" + cropFileName;
			File protraitFile = new File(protraitPath);
			Uri cropUri = Uri.fromFile(protraitFile);
			return cropUri;
		}
		
}